<?php

class system_queue_consumer_proc_thread {

    public $resource;

    public $pipes;

    private $startTime;

    function __construct($queue_name, $phpExec = '') {
        if ($phpExec) {
            $executable = $phpExec;
        } elseif (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") {
            $executable = dirname(ini_get('extension_dir')) . '/php';
            $executable = file_exists($executable) ? $executable : 'php';
        } else {
            $executable = PHP_BINDIR . '/php';
            $executable = file_exists($executable) ? $executable : 'php';
        }
        
        $script = SCRIPT_DIR . "/queue/queuescript.php";
        
        $descriptorspec = array(
            0 => array(
                'pipe',
                'r'
            ),
            1 => array(
                'file',
                '/dev/null',
                'a'
            ),
            2 => array(
                'pipe',
                '/dev/null',
                'a'
            )
        );
        echo $executable . " " . $script . " " . $queue_name . "\n";
        
        $i = 0;
        while (($this->resource = proc_open($executable . " " . $script . " " . $queue_name, $descriptorspec, $this->pipes, NULL, $_ENV)) === null) {
            $i++;
            if ($i > 2) {
                throw new Exception(' cannot create new proccess for consume queue.', 30001);
            }
        }
        
        $this->startTime = time();
    }

    function isRunning() {
        $status = proc_get_status($this->resource);
        return $status["running"];
    }

    function isOverExecuted($max_exec_time) {
        if (($this->startTime + $max_exec_time) < time())
            return true;
        else
            return false;
    }
}